package com.spukmk2me.spukmk2mesceneeditor.gui;

import java.awt.Frame;
import javax.swing.JDialog;

import com.spukmk2me.scene.ISceneNode;
import com.spukmk2me.scene.NullSceneNode;
import com.spukmk2me.scene.ImageSceneNode;
import com.spukmk2me.scene.LineSceneNode;
import com.spukmk2me.scene.SpriteSceneNode;
import com.spukmk2me.scene.StringSceneNode;
import com.spukmk2me.scene.TiledLayerSceneNode;
import com.spukmk2me.scene.complex.ClippingSceneNode;
import com.spukmk2me.scene.complex.ViewportSceneNode;

public final class AddSceneNodeDialog extends JDialog
{
    public AddSceneNodeDialog( DisplayedSceneTree tree, ISceneNode parentNode,
        Frame parent )
    {
        super( parent, true );
        m_tree          = tree;
        m_parentNode    = parentNode;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_typeList = new javax.swing.JList();
        m_addButton = new javax.swing.JButton();
        m_cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add scene node");
        setModal(true);
        setResizable(false);

        jLabel1.setText("Select:");

        m_typeList.setSelectionMode( javax.swing.ListSelectionModel.SINGLE_SELECTION );
        m_typeList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = {
                "Null scene node",
                "Image scene node",
                "Sprite scene node",
                "String scene node",
                "Tiled scene node",
                "Clipping scene node",
                "Viewport scene node",
                "Line scene node"
            };

            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(m_typeList);

        m_addButton.setText("Add");
        m_addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_addButtonActionPerformed(evt);
            }
        });

        m_cancelButton.setText("Cancel");
        m_cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(137, Short.MAX_VALUE)
                .addComponent(m_cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_addButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_addButton)
                    .addComponent(m_cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_m_cancelButtonActionPerformed

    private void m_addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_addButtonActionPerformed

        int index = m_typeList.getSelectedIndex();

        if ( index != -1 )
        {
            ISceneNode newNode = null;

            switch ( index )
            {
                case 0:
                    newNode = new NullSceneNode();
                    break;

                case 1:
                    newNode = new ImageSceneNode( null );
                    break;

                case 2:
                    newNode             = new SpriteSceneNode( null );
                    newNode.c_infoData  = ((SpriteSceneNode)newNode).
                        new SpriteSceneNodeInfoData();
                    break;

                case 3:
                    newNode             = new StringSceneNode();
                    newNode.c_infoData  = ((StringSceneNode)newNode).
                        new StringSceneNodeInfoData();
                    break;

                case 4:
                    newNode             = new TiledLayerSceneNode();
                    newNode.c_infoData  = ((TiledLayerSceneNode)newNode).
                        new TiledLayerSceneNodeInfoData();
                    break;
                    
                case 5:
                    newNode             = new ClippingSceneNode();
                    newNode.c_infoData  = ((ClippingSceneNode)newNode).
                        new ClippingSceneNodeInfoData();
                    break;

                case 6:
                    newNode = new ViewportSceneNode();
                    break;

                case 7:
                    newNode = new LineSceneNode();
                    break;
            }

            if ( newNode != null )
            {
                newNode.c_visible   = false;
                newNode.c_enable    = true;
                m_tree.getSceneTreeModel().addNode( newNode, m_parentNode );
            }

            this.dispose();
        }
    }//GEN-LAST:event_m_addButtonActionPerformed

    private DisplayedSceneTree  m_tree;
    private ISceneNode          m_parentNode;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_addButton;
    private javax.swing.JButton m_cancelButton;
    private javax.swing.JList m_typeList;
    // End of variables declaration//GEN-END:variables
}
